/***************************************************************************
 创建者：          hualei
 开始时间：      2018.09.10
 copyright:(C)    深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************/
#include "../../programView/programview.h"
#include "modbuswriteinstructionview.h"
#include "ui_modbuswriteinstructionview.h"

ModbusWriteInstructionView::ModbusWriteInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusWriteInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);
}

void ModbusWriteInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->note_GBox->hide();
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void ModbusWriteInstructionView::getInput()
{
    noteStr = ui->note_lineEdit->text();
    modbusID = ui->modbusID_Edit->text();
    writeRegAddress = ui->RegAddressID_Edit->text().toInt();
    JMP_LB_Value = ui->JMP_LB_Edit->text();
    writeValue = ui->NoConstValue_lineEdit->text().toInt();
    writeConstValue = ui->ConstValue_lineEdit->text().toInt();
    switch(ui->INTERNET_RS485_ComBox->currentIndex())
    {
    case 0:
    {
        selectionModbusType=E_SELECTION_INTERNET;
        break;
    }
    case 1:
    {
        selectionModbusType=E_SELECTION_RS485;
        break;
    }
    }

    switch(ui->MASTER_SLAVE_ComboBox->currentIndex())
    {
    case 0:
    {
        selectionModbusMode=E_SELECTION_MASTER;
        break;
    }
    case 1:
    {
        selectionModbusMode=E_SELECTION_SLAVE;
        break;
    }
    }
    if((selectionModbusType == E_SELECTION_INTERNET) && (selectionModbusMode == E_SELECTION_MASTER))
    {
        modbusWriteType = E_MODBUS_WRITE_TYPE_INTERNET_MASTER;
    }
    else if((selectionModbusType == E_SELECTION_INTERNET) && (selectionModbusMode == E_SELECTION_SLAVE))
    {
        modbusWriteType = E_MODBUS_WRITE_TYPE_INTERNET_SLAVE;
    }
    else if((selectionModbusType == E_SELECTION_RS485) && (selectionModbusMode == E_SELECTION_MASTER))
    {
        modbusWriteType = E_MODBUS_WRITE_TYPE_RS485_MASTER;
    }
    else if((selectionModbusType == E_SELECTION_RS485) && (selectionModbusMode == E_SELECTION_SLAVE))
    {
        modbusWriteType = E_MODBUS_WRITE_TYPE_RS485_SLAVE;
    }
    else
    {
        modbusWriteType = E_MODBUS_WRITE_TYPE_ERROR;
    }

}

void ModbusWriteInstructionView::updateView()
{
    ui->note_lineEdit->setText(noteStr);
    ui->RegAddressID_Edit->setText(QString::number(writeRegAddress));
    ui->modbusID_Edit->setText(modbusID);
    ui->JMP_LB_Edit->setText(JMP_LB_Value);

    if(selectionWriteValueType == E_SELECTION_CONST)
    {
        ui->CONST_radioButton->setChecked(true);
        ui->ConstValue_lineEdit->setEnabled(true);

        ui->ConstValue_lineEdit->setText(QString::number(writeConstValue));
        ui->NoConstValue_lineEdit->setText(QString::number(writeValue));
        ui->NoConstValue_lineEdit->setEnabled(false);
    }
    else if(selectionWriteValueType == E_SELECTION_NO_CONST )
    {
        ui->NoConst_radioButton->setChecked(true);
        ui->NoConstValue_lineEdit->setEnabled(true);

        ui->NoConstValue_lineEdit->setText(QString::number(writeValue));
        ui->ConstValue_lineEdit->setText(QString::number(writeConstValue));
        ui->ConstValue_lineEdit->setEnabled(false);

    }

    if((selectionModbusType == E_SELECTION_INTERNET) && (selectionModbusMode == E_SELECTION_MASTER))
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(0);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(0);
//        ui->INTERNET_radioButton->setChecked(true);
//        ui->MASTER_radioButton->setChecked(true);
        modbusWriteType = E_MODBUS_WRITE_TYPE_INTERNET_MASTER;
    }
    else if((selectionModbusType == E_SELECTION_INTERNET) && (selectionModbusMode== E_SELECTION_SLAVE))
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(0);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(1);
//        ui->INTERNET_radioButton->setChecked(true);
//        ui->SLAVE_radioButton->setChecked(true);
        modbusWriteType = E_MODBUS_WRITE_TYPE_INTERNET_SLAVE;
    }
    else if((selectionModbusType == E_SELECTION_RS485) && (selectionModbusMode == E_SELECTION_MASTER))
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(1);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(0);
//        ui->RS485_radioButton->setChecked(true);
//        ui->MASTER_radioButton->setChecked(true);
        modbusWriteType = E_MODBUS_WRITE_TYPE_RS485_MASTER;
    }
    else if((selectionModbusType == E_SELECTION_RS485) && (selectionModbusMode == E_SELECTION_SLAVE))
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(1);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(1);
//        ui->RS485_radioButton->setChecked(true);
//        ui->SLAVE_radioButton->setChecked(true);
        modbusWriteType = E_MODBUS_WRITE_TYPE_RS485_SLAVE;
    }

}

void ModbusWriteInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/modbusWriteViewEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);

}

//void ModbusWriteInstructionView::on_INTERNET_radioButton_clicked()
//{
//    selectionModbusType = E_SELECTION_INTERNET;
//}

//void ModbusWriteInstructionView::on_RS485_radioButton_clicked()
//{
//    selectionModbusType = E_SELECTION_RS485;
//}

//void ModbusWriteInstructionView::on_MASTER_radioButton_clicked()
//{
//    selectionModbusMode= E_SELECTION_MASTER;
//}

//void ModbusWriteInstructionView::on_SLAVE_radioButton_clicked()
//{
//    selectionModbusMode = E_SELECTION_SLAVE;
//}

void ModbusWriteInstructionView::on_CONST_radioButton_clicked()
{
    selectionWriteValueType = E_SELECTION_CONST;
    ui->ConstValue_lineEdit->setEnabled(true);
    ui->NoConstValue_lineEdit->setEnabled(false);
}

void ModbusWriteInstructionView::on_NoConst_radioButton_clicked()
{
    selectionWriteValueType = E_SELECTION_NO_CONST;
    ui->ConstValue_lineEdit->setEnabled(false);
    ui->NoConstValue_lineEdit->setEnabled(true);
}

void ModbusWriteInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNoteSignal();
}

void ModbusWriteInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void ModbusWriteInstructionView::on_save_Btn_clicked()
{
    getInput();
    emit finishEditInstruction();
    emit saveInstruction(instruction);
}
